﻿using System;
using System.Collections.Generic;
using System.Text;
/*
    Because the service broker stuff is so detailed and is ported from a separate application
    It would tend to swamp the original dbview UI. 
    The decision is to hide the UI away in right click menus that are only available
    when navigating among the Service broker views.

	Broker commands to do
	- queue entry
	- script drop this and that
	- script end conversations
	- add builder dialog 
	- toggle queue activation/status
	- search source for message text?


*/
namespace SqlServerDriver
{
	class SqlServerBrokerPopupBuilder
		{
		// expose commands the class knows about
		public static bool IsBrokerCommand(DbView.Command command)
		{
			// must be a custom command
			if (command.IsCustomCommand)
			{
				return (command.Name == Cmd.ServiceBroker) ? true:
				(command.Name == Cmd.BrokerMessages) ? true:
				(command.Name == Cmd.BrokerContracts) ? true:
				(command.Name == Cmd.BrokerQueues) ? true:
				(command.Name == Cmd.BrokerServices) ? true:
				(command.Name == Cmd.BrokerRoutes) ? true:
				(command.Name == Cmd.BrokerEndpoints) ? true:
				(command.Name == Cmd.BrokerXmitQueue) ? true:
				(command.Name == Cmd.BrokerDatabases) ? true:
				(command.Name == Cmd.BrokerQueueItems) ? true :
				(command.Name == Cmd.BrokerQueueItemsNoData) ? true :
				(command.Name == Cmd.BrokerScriptDropMessage) ? true :
				(command.Name == Cmd.BrokerScriptDropContract) ? true :
				(command.Name == Cmd.BrokerScriptDropService) ? true :
				(command.Name == Cmd.BrokerScriptDropQueue) ? true :
				(command.Name == Cmd.BrokerScriptEndConversation) ? true :
				(command.Name == Cmd.BrokerScriptToggleActivation) ? true :
				(command.Name == Cmd.BrokerScriptToggleStatus) ? true :
				(command.Name == Cmd.BrokerBuilderDialog) ? true :
				false;
			}
			return false;
		}

		public static bool IsBrokerScriptingCommand(DbView.Command command)
		{
			// must be a custom command
			if (command.IsCustomCommand)
			{
				return (command.Name == Cmd.BrokerScriptDropMessage) ? true :
						(command.Name == Cmd.BrokerScriptDropContract) ? true :
						(command.Name == Cmd.BrokerScriptDropService) ? true :
						(command.Name == Cmd.BrokerScriptDropQueue) ? true :
						(command.Name == Cmd.BrokerScriptEndConversation) ? true :
						(command.Name == Cmd.BrokerScriptToggleActivation) ? true :
						(command.Name == Cmd.BrokerScriptToggleStatus) ? true :
				false;
			}
			return false;
		}

		public static DbViewDriver.CustomMenuItem BuildPopupMenu(DbView.Command command, DbViewDriver.CustomMenuItem root)
		{
			// [Specialise: broker subsystem popup menus]
			switch (command.Cmd)
			{
				case DbView.Commands.Cmnd.Extension:
					switch (command.Name)
					{
						case Cmd.BrokerQueues:
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Messages in Queue", Cmd.BrokerQueueItems);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Messages in Queue (w/o data)", Cmd.BrokerQueueItemsNoData);
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddSeparator(root);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Toggle Activation", Cmd.BrokerScriptToggleActivation);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Toggle Status", Cmd.BrokerScriptToggleStatus);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Script Drop Queue", Cmd.BrokerScriptDropQueue);
						SqlServerPopupBuilder.AddSeparator(root);
						ScriptBrokerMenu(command, root);
						SqlServerPopupBuilder.AddShowSql(root);
						break;
						case Cmd.BrokerMessages:
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddSeparator(root);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Script Drop Message", Cmd.BrokerScriptDropMessage);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Script Drop Contract", Cmd.BrokerScriptDropContract);
						SqlServerPopupBuilder.AddSeparator(root);
						ScriptBrokerMenu(command, root);
						SqlServerPopupBuilder.AddShowSql(root);
						break;
						case Cmd.BrokerServices:
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddSeparator(root);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Script Drop Service", Cmd.BrokerScriptDropService);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Script Drop Queue", Cmd.BrokerScriptDropQueue);
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddSeparator(root);
						ScriptBrokerMenu(command, root);
						SqlServerPopupBuilder.AddShowSql(root);
						break;
						case Cmd.BrokerEndpoints:
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Script End Conversation", Cmd.BrokerScriptEndConversation);
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddSeparator(root);
						ScriptBrokerMenu(command, root);
						SqlServerPopupBuilder.AddShowSql(root);
						break;
						case Cmd.BrokerXmitQueue:
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddCustomMenuItem(root, "Script End Conversation", Cmd.BrokerScriptEndConversation);
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddSeparator(root);
						ScriptBrokerMenu(command, root);
						SqlServerPopupBuilder.AddShowSql(root);
						break;
						default:
						SqlServerPopupBuilder.AddProfile(root);
						SqlServerPopupBuilder.AddSeparator(root);
						ScriptBrokerMenu(command, root);
						SqlServerPopupBuilder.AddShowSql(root);
						break;
					}
					break;
				default:
					// should not reallly arrive here 
					SqlServerPopupBuilder.AppendDefault(root, false);
					break;
			}
			return root;
		}

		// all broker views give access to the items below as they really belong on the main menu,
		// but are not shown at present.
		static void ScriptBrokerMenu(DbView.Command command, DbViewDriver.CustomMenuItem root)
		{
			// prefix all the standard items to show that they are really main not context items.
			String menuPrefix = "Service Broker: ";

			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Messages", Cmd.BrokerMessages).IsRowIndependent = true;
			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Contracts", Cmd.BrokerContracts).IsRowIndependent = true;
			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Queues", Cmd.BrokerQueues).IsRowIndependent = true;
			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Services", Cmd.BrokerServices).IsRowIndependent = true;
			SqlServerPopupBuilder.AddSeparator(root);
			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Transmission Queue", Cmd.BrokerXmitQueue).IsRowIndependent = true;
			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Endpoints", Cmd.BrokerEndpoints).IsRowIndependent = true;
			SqlServerPopupBuilder.AddSeparator(root);
			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Routes", Cmd.BrokerRoutes).IsRowIndependent = true;
			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Database settings", Cmd.BrokerDatabases).IsRowIndependent = true;
			SqlServerPopupBuilder.AddSeparator(root);
			SqlServerPopupBuilder.AddCustomMenuItem(root, menuPrefix + "Add broker objects", Cmd.BrokerBuilderDialog).IsRowIndependent = true;
			SqlServerPopupBuilder.AddSeparator(root);
		}

		public static String MakeBrokerCommand(String template, DbView.Command command, String filterString)
		{
			if (command.Name == Cmd.BrokerQueueItems)
				return String.Format(template, command[DbView.Command.TableParameterName(0)], filterString);
			if (command.Name == Cmd.BrokerQueueItemsNoData)
				return String.Format(template, command[DbView.Command.TableParameterName(0)], filterString);
			return template;
		}

		public static void CommandParameters(DbView.Command command, DbView.DataTableExtraInfoManager mgr)
		{
			// for popups that require items not in column 0 use this
			if (command.Name == Cmd.BrokerEndpoints)
				mgr.SetParam(mgr.Table.Columns[6], CmdParam.ConversationHandle);
			if (command.Name == Cmd.BrokerXmitQueue)
				mgr.SetParam(mgr.Table.Columns[4], CmdParam.ConversationHandle);
			if (command.Name == Cmd.BrokerMessages)
				mgr.SetParam(mgr.Table.Columns[1], CmdParam.ContractName);
			if (command.Name == Cmd.BrokerServices)
				mgr.SetParam(mgr.Table.Columns[1], CmdParam.QueueName);
			if (command.Name == Cmd.BrokerQueues)
			{
				mgr.SetParam(mgr.Table.Columns[4], CmdParam.ActivationFlag);
				mgr.SetParam(mgr.Table.Columns[6], CmdParam.StatusFlag);
				mgr.SetParam(mgr.Table.Columns[3], CmdParam.ProcedureName);
			}
		}
	}
}
